package io.renren.modules.location.service.impl;

import io.renren.common.config.FileServerConfig;
import io.renren.common.exception.RRException;
import io.renren.common.utils.DfsUtil;
import io.renren.common.utils.FileServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import io.renren.modules.location.dao.UserRegisterDao;
import io.renren.modules.location.entity.UserRegisterEntity;
import io.renren.modules.location.service.UserRegisterService;
import sun.dc.pr.PRException;


@Service("userRegisterService")
public class UserRegisterServiceImpl implements UserRegisterService {
	@Autowired
	private UserRegisterDao userRegisterDao;

    @Autowired
	private FileServerConfig fileServerConfig;
	
	@Override
	public UserRegisterEntity queryObject(String id){
		return userRegisterDao.queryObject(id);
	}
	
	@Override
	public List<UserRegisterEntity> queryList(Map<String, Object> map){
		return userRegisterDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return userRegisterDao.queryTotal(map);
	}
	
	@Override
	public void save(UserRegisterEntity userRegister){
        uploadFile(userRegister);
        userRegister.setId(UUID.randomUUID().toString().replaceAll("-",""));
        userRegister.setCreateTime(new Date());
        userRegisterDao.save(userRegister);
	}
	
	@Override
	public void update(UserRegisterEntity userRegister){
        uploadFile(userRegister);
		userRegisterDao.update(userRegister);
	}
	
	@Override
	public void delete(String id){
		userRegisterDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		userRegisterDao.deleteBatch(ids);
	}


    private void uploadFile(UserRegisterEntity userRegister) {
        if(!userRegister.getBankScanUrl().isEmpty()){
            try {
                FileServerResponse bankresponse = DfsUtil.uploadFileByMultipartFile(userRegister.getBankScanUrl(), fileServerConfig.getFileUploadUrl());
                userRegister.setBankFilePath(bankresponse.getRealHttpUrl());
            } catch (IOException e) {
                throw new RRException("上传银行卡扫描件失败！");
            }
        }
        if (!userRegister.getIdScanUrl().isEmpty()) {
            try {
                FileServerResponse idcardResponse = DfsUtil.uploadFileByMultipartFile(userRegister.getIdScanUrl(), fileServerConfig.getFileUploadUrl());
                userRegister.setIdcardFilepath(idcardResponse.getRealHttpUrl());
            } catch (IOException e) {
                throw new RRException("上传身份证扫描件失败！");
            }
        }
        if (!userRegister.getOtherDocument().isEmpty()) {
            try {
                FileServerResponse otherResponse = DfsUtil.uploadFileByMultipartFile(userRegister.getOtherDocument(), fileServerConfig.getFileUploadUrl());
                userRegister.setOtherFilePath(otherResponse.getRealHttpUrl());
            } catch (IOException e) {
                throw new RRException("上传其他资料失败！");
            }
        }
    }
}
